// pages/profile/profile.js
import http from "../../../../utils/http"
import {storebehabior} from './behavior'
import {userstore} from '../../../../store/userstore'
import {showtoast} from '../../../../utils/extendapi'
Page({
  behaviors:[storebehabior],
  // 页面的初始数据
  data: {
    isShowPopup: false, // 控制更新用户昵称的弹框显示与否
    userinfo:{
      avatarurl:""
    },
    nickname:"尚硅谷"
  },

  // 显示修改昵称弹框
  onUpdateNickName() {
    this.setData({
      isShowPopup: true,
      nickname:this.data.nickname
    })
  },

  // 弹框取消按钮
  cancelForm() {
    this.setData({
      isShowPopup: false

    })
  },
  async chooseAvatar(e){
  const res=await http.upload("/api/avatar",e.detail.avatarUrl)
  this.setData({
    "userinfo.avatarurl":res.avatarurl
  })
  },
  async getnickname(){
    const res=await http.get("/api/nickname")
    this.setData({
      nickname:res.data.nickname,
      "userinfo.name":res.data.nickname
    })
  },
  async onShow(){
    this.getnickname()
  },
  async bindsubmit(e){
    const {nickname}=e.detail.value
    const res=await http.post(`/api/updataname?nickname=${nickname}`)
    await this.getnickname()
    userstore.setuserinfo(this.data.userinfo)
    this.setData({
      isShowPopup:false
    })
  },
  success(){
    showtoast({
      title:"默认会保存，不用手动保存"
    })
  }
})
